package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.MakeFriendManager;
import com.itheima.app.manager.MessageManager;
import com.itheima.app.manager.UserManger;
import com.itheima.domain.vo.HuanXinVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class MessageController {

    @Autowired
    private MessageManager messageManager;
    //查询陌生人问题
    @GetMapping("/tanhua/strangerQuestions")
    public ResponseEntity strangerQuestions(Long userId) throws IOException {

        return messageManager.strangerQuestions(userId);
    }


    //app获得环信数据
    @GetMapping("/huanxin/user")
    public ResponseEntity getHuanxinUser() throws IOException {
        Long userId = UserHolder.getUserId(); //当前的登陆用户人id
        //构建返回的环信数据
        HuanXinVo huanXinVo = new HuanXinVo();
        huanXinVo.setUsername("HX"+userId);
        huanXinVo.setPassword("123456");
        System.out.println("app端获得环信的数据:"+huanXinVo);
        return ResponseEntity.ok(huanXinVo);
    }

    //回复陌生人问题
    @PostMapping("/tanhua/strangerQuestions")
    public ResponseEntity strangerQuestions(@RequestBody Map<String,String> map) throws IOException {
        String userId = map.get("userId"); //佳人的id
        String reply = map.get("reply"); //回复的内容
        return messageManager.strangerQuestions(userId , reply);
    }

    //联系人添加
    @PostMapping("/messages/contacts")
    public ResponseEntity addContacts(@RequestBody Map<String,String> map) throws IOException {
        String jiarenId = map.get("userId");
        return messageManager.addContacts(jiarenId);
    }
    //联系人列表
    @GetMapping("/messages/contacts")
    public ResponseEntity contactList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize
            ) throws IOException {

        return messageManager.findContactList(page , pagesize);
    }

    @Autowired
    private UserManger userManger;

    // 根据环信id查询用户信息
    @GetMapping("/messages/userinfo")
    public ResponseEntity findByUserInfoVo(String huanxinId) {  // 前端存储的 HX99
        String userId = huanxinId.replace("HX", "");
        return userManger.findUserInfoById(userId , null);
    }



    // 点赞列表
    @GetMapping("/messages/likes")
    public ResponseEntity likes(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize
    ) {
        Long commentType = 1L;
        return messageManager.findInfo(page , pagesize , commentType);
    }
    // 评论列表
    @GetMapping("/messages/comments")
    public ResponseEntity comments(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize
    ) {
        Long commentType = 2L;
        return messageManager.findInfo(page , pagesize , commentType);
    }

    // 喜欢列表
    @GetMapping("/messages/loves")
    public ResponseEntity loves(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize
    ) {
        Long commentType = 3L;
        return messageManager.findInfo(page , pagesize , commentType);
    }


}
